package com.appspile.android.ANMP.ApileIQCA.common;

import android.util.Log;


public class JSonObject
{
	private String m_keys[];
	private Object m_values[];

	private int m_maxPairIndex;
	private final static int INITIAL_PAIRS_SLOTS_AMOUNT 	= 10;
	private final static int PAIRS_SLOTS_AMOUNT_INCREMENT 	= 5;

	public final static String TAG = "JSONOBJECT";
	//------------------------------------------------------------------------------
	/// Public constructor
	//------------------------------------------------------------------------------
	public JSonObject()
	{
		m_maxPairIndex = 0;
		m_keys = new String[INITIAL_PAIRS_SLOTS_AMOUNT];
		m_values = new Object[INITIAL_PAIRS_SLOTS_AMOUNT];
		// isValid = true;
	}

	//------------------------------------------------------------------------------
	/// Return amount of Keys in JSon
	//------------------------------------------------------------------------------
	public int GetKeysNum()
	{
		return m_maxPairIndex;
	}

	//------------------------------------------------------------------------------
	/// Return Keys array. Use only to enumerate through all keys
	//------------------------------------------------------------------------------
	public String[] GetKeySet()
	{
		return m_keys;
	}

	//------------------------------------------------------------------------------
	/// Return Values array
	//------------------------------------------------------------------------------
	public Object[] GetValueSet()
	{
		return m_values;
	}


	//------------------------------------------------------------------------------
	/// Public constructor. Creates JSonObject from specified string
	/// @param json - input string to create JSonObject from
	//------------------------------------------------------------------------------
	public JSonObject(String json)
	{
		FromString(json);
	}

	//------------------------------------------------------------------------------
	/// Creates JSonObject from specified string
	/// @param json - input string to create JSonObject from
	//------------------------------------------------------------------------------
	public void FromString(String json)
	{
		if (json != null)
		{
			m_maxPairIndex = 0;
			m_keys = new String[INITIAL_PAIRS_SLOTS_AMOUNT];
			m_values = new Object[INITIAL_PAIRS_SLOTS_AMOUNT];

			if (json.startsWith("["))
			{
				json = "{\"json\":" + json + "}";
			}

			ParseJsonString(json, 0, json.length());
		}
	}

	//------------------------------------------------------------------------------
	/// Check if input string is valid json string
	///
	/// @return True if input string is valid
	//------------------------------------------------------------------------------
	// public static boolean IsValidJSonString (String jsonString)
	// {
	// int cnt = 0;
	// char c;
	// for (int i = 0; i < jsonString.length(); i++)
	// {
	// c = jsonString.charAt(i);
	// if (c == '{')
	// cnt++;
	// else if (c == '}')
	// cnt--;
	// }

	// return cnt == 0;
	// }

	//------------------------------------------------------------------------------
	/// Check if current JSonObject is valid
	///
	/// @return True if current JSonObject is valid
	//------------------------------------------------------------------------------
	// public boolean IsValid ()
	// {
	// return isValid;
	// }

	//------------------------------------------------------------------------------
	/// Parses json string
	///
	/// @param jsonString - input string to parse
	/// @param startIndex - index to start from
	/// @param endIndex - 	end index to search to
	//------------------------------------------------------------------------------
	private void ParseJsonString(String jsonString, int startIndex, int endIndex)
	{
		int valueEndIndex;
		try
		{
			while (true)
			{
				String key = null;
				Object value = null;

				key = NextKey(jsonString, startIndex, endIndex);
				if (key == null)
				{
					break;
				}

				// "+ 1" is for " symbols
				startIndex = jsonString.indexOf(key, startIndex) + key.length() + 1;

				valueEndIndex = GetValueEndIndex(jsonString, startIndex, endIndex);
				value = ParseValue(jsonString, startIndex, valueEndIndex);
				startIndex = valueEndIndex;

				Put(key, value);
			}
		}
		catch (Exception e)
		{
			Log.w(TAG,e);
		}
	}

	//------------------------------------------------------------------------------
	/// Find JSon value end index in string
	///
	/// @param jsonString - input string to search in
	/// @param startIndex - start index to search from
	/// @param endIndex - end index to search to
	///
	/// @return index of symbol in string at which value ends
	//------------------------------------------------------------------------------
	private int GetValueEndIndex(String jsonString, int startIndex, int endIndex)
	{
		char c;
		char startChar = 0;
		int cnt = 0;
		boolean valueSymbolFound = false;

		for (int i = startIndex; i < endIndex; i++)
		{
			c = jsonString.charAt(i);

			if (c != ' ' && c != ':' && c != ',' && c != '}' && c != ']')
			{
				valueSymbolFound = true;
			}

			switch (c)
			{
			case '\\':
				i++;
				break;
			case '"':
			case '\'':
				if (startChar == 0)
				{
					startChar = c;
				}
				else if (c == startChar)
				{
					return i + 1;
				}
				break;
			case '{':
			case '[':
				if (startChar == 0 || startChar == c)
				{
					startChar = c;
					cnt++;
				}
				break;
			case '}':
				if (startChar == '{')
				{
					cnt--;
				}

				if (cnt == 0 && valueSymbolFound && startChar != '"')
				{
					return i + 1;
				}
				break;
			case ']':
				if (startChar == '[')
				{
					cnt--;
				}

				if (cnt == 0 && valueSymbolFound && startChar != '"')
				{
					return i + 1;
				}
				break;
			case ',':
				if (startChar == 0 && valueSymbolFound)
				{
					return i + 1;
				}
				break;

			default:
				break;
			}
		}
		return -1;
	}

	//------------------------------------------------------------------------------
	/// Determines the type of value(String, another JSon, array) and parses it
	///
	/// @param jsonString - input string to search in
	/// @param startIndex - value start index
	/// @param endIndex - value end index
	///
	/// @return value as Object
	//------------------------------------------------------------------------------
	private Object ParseValue(String jsonString, int startIndex, int endIndex) throws Exception
	{
		char c = 0;
		int i;
		for (i = startIndex; i < endIndex; i++)
		{
			c = jsonString.charAt(i);

//			if (c != ' ' && c != ':' && c != '"' && c != '\'' && c != ',')
			if (c != ' ' && c != ':' && c != ',')
			{
				break;
			}
		}

		switch (c)
		{
		case '"':
		case '\'':
			return NextString(jsonString, i + 1, endIndex);
		case '{':
			return new JSonObject(jsonString.substring(i, endIndex));
		case '[':
			return ParseArray(jsonString, i + 1, endIndex);

		default:
			return NextString(jsonString, i, endIndex);
		}
	}

	//------------------------------------------------------------------------------
	/// Parses substring from startIndex to endIndex as array
	///
	/// @param jsonString - input string to parse
	/// @param startIndex - array start index
	/// @param endIndex - array end index
	///
	/// @return array of objects
//------------------------------------------------------------------------------
	private Object ParseArray(String jsonString, int startIndex, int endIndex) throws Exception
	{
		int valueEndIndex = startIndex;
		int valueStartIndex;
		int arraySize = -1;
		int index = 0;
		Object[] array = null;
		do
		{
			valueEndIndex = GetValueEndIndex(jsonString, valueEndIndex, endIndex);
			arraySize++;
		}
		while (valueEndIndex != -1);

		if (arraySize >= 0)
		{
			array = new Object[arraySize];
			valueStartIndex = startIndex;
			valueEndIndex = GetValueEndIndex(jsonString, startIndex, endIndex);

			while (valueEndIndex >= 0)
			{
				array[index++] = ParseValue(jsonString, valueStartIndex, valueEndIndex);
				valueStartIndex = valueEndIndex;
				valueEndIndex = GetValueEndIndex(jsonString, valueEndIndex, endIndex);
			}
		}

		return array;
	}

	//------------------------------------------------------------------------------
	/// Returns next key from pair key : value
	///
	/// @param jsonString - input string to parse
	/// @param startIndex - start index to process
	/// @param endIndex - 	end index to process
	///
	/// @return key string
	//------------------------------------------------------------------------------
	private String NextKey(String jsonString, int startIndex, int endIndex)
	{
		return GetStringBetweenQuotes(jsonString, startIndex, endIndex);
	}

	//------------------------------------------------------------------------------
	/// Returns string between quotes
	///
	/// @param jsonString - input string to process
	/// @param startIndex - start index to process
	/// @param endIndex - 	end index to process
	///
	/// @return string between quotes
	//------------------------------------------------------------------------------
	private String GetStringBetweenQuotes(String jsonString, int startIndex, int endIndex)
	{
		char c;
		boolean addChar = false;
		boolean addSpecialChar = false;
		StringBuffer tmpSB = new StringBuffer();

		for (int i = startIndex; i < endIndex; i++)
		{
			c = jsonString.charAt(i);

			if (addSpecialChar)
			{
				AddSpecialChar(tmpSB, c);
				continue;
			}

			switch (c)
			{
			case '"':
			case '\'':
				addChar = !addChar;

				if (!addChar)
				{
					return tmpSB.toString();
				}
				break;
			case '\\':
				addSpecialChar = true;
				break;
			default:
				if (addChar)
				{
					tmpSB.append(c);
				}
			}
		}
		return null;
	}

	//------------------------------------------------------------------------------
	/// Returns substring from startIndex to endIndex
	///
	/// @param jsonString - input string to process
	/// @param startIndex - start index to process
	/// @param endIndex - 	end index to process
	///
	/// @return substring in specified bounds
	//------------------------------------------------------------------------------
	private String NextString(String jsonString, int startIndex, int endIndex) throws Exception
	{
		if (endIndex <= startIndex)
		{
			return "";
		}
		return jsonString.substring(startIndex, endIndex - 1).trim();
	}

	//------------------------------------------------------------------------------
	/// Adds special char to string buffer
	///
	/// @param tmpSB - string buffer to add to
	/// @param c - char to add
	///
	//------------------------------------------------------------------------------
	private void AddSpecialChar(StringBuffer tmpSB, char c)
	{
		switch (c)
		{
		case 'b':
			tmpSB.append('\b');
			break;
		case 't':
			tmpSB.append('\t');
			break;
		case 'n':
			tmpSB.append('\n');
			break;
		case 'f':
			tmpSB.append('\f');
			break;
		case 'r':
			tmpSB.append('\r');
			break;
//            case 'u':
//            	tmpSB.append((char)Integer.parseInt(Next(4), 16));
//                break;
//            case 'x' :
//            	tmpSB.append((char) Integer.parseInt(Next(2), 16));
//                break;
		default:
			tmpSB.append(c);
		}
	}

	//------------------------------------------------------------------------------
	/// Associates the specified value with the specified key in this JSonObject
	///
	/// @param key - key with which the specified value is to be associated
	/// @param value - value to be associated with the specified key
	//------------------------------------------------------------------------------
	public void Put(String key, Object value)
	{
		int index = GetIndex(key);
		if (index != -1)
		{
			m_values[index] = value;
		}
		else
		{
			m_keys[m_maxPairIndex] = key;
			m_values[m_maxPairIndex] = value;
			m_maxPairIndex++;
			if (m_maxPairIndex == m_keys.length)
			{
				IncreaseSlotsAmount(PAIRS_SLOTS_AMOUNT_INCREMENT);
			}
		}
	}

	//------------------------------------------------------------------------------
	/// Increases slot amounts to store key:value pairs
	///
	/// @param size - amount of slots to add
	//------------------------------------------------------------------------------
	private void IncreaseSlotsAmount(int size)
	{
		String[] newKeys = new String[m_maxPairIndex + size];
		Object[] newValues = new Object[m_maxPairIndex + size];
		System.arraycopy(m_keys, 0, newKeys, 0, m_maxPairIndex);
		System.arraycopy(m_values, 0, newValues, 0, m_maxPairIndex);
		m_keys = newKeys;
		m_values = newValues;
	}

	//------------------------------------------------------------------------------
	/// Associates the specified value with the specified key in this JSonObject
	///
	/// @param key - key with which the specified value is to be associated
	/// @param value - value to be associated with the specified key
	//------------------------------------------------------------------------------
	public void Put(String key, int value)
	{
		Put(key, String.valueOf(value));
	}

	//------------------------------------------------------------------------------
	/// Associates the specified value with the specified array of values in this JSonObject
	///
	/// @param key - key with which the specified value is to be associated
	/// @param values - array of values to be associated with the specified key
	//------------------------------------------------------------------------------
	public void Put(String key, int[] values)
	{
		Object s[] = new Object[values.length];
		for (int i = 0; i < s.length; i++)
		{
			s[i] = String.valueOf(values[i]);
		}
		Put(key, s);
	}

	//------------------------------------------------------------------------------
	/// Check if such key exist in JSon
	///
	/// @param key - key to search for
	//------------------------------------------------------------------------------
	public boolean IsKeyExist(String key)
	{
		return GetIndex(key) != -1;
	}

	//------------------------------------------------------------------------------
	/// Returns the index of specified key in "map"
	///
	/// @param key - key to search for
	//------------------------------------------------------------------------------
	private int GetIndex(String key)
	{
		if (key == null)
		{
			return -1;
		}
		for (int i = 0; i < m_maxPairIndex; i++)
		{
			if (m_keys[i].equals(key))
			{
				return i;
			}
		}
		return -1;
	}

	//------------------------------------------------------------------------------
	/// Returns the value with the corresponding key or null if no value is associated with this key.
	///
	/// @param key - key to search for
	///
	/// @return Object value
	//------------------------------------------------------------------------------
	public Object GetValue(String key)
	{

		int index = GetIndex(key);
		if (index != -1)
		{
			return m_values[index];
		}
		return null;
	}

	//------------------------------------------------------------------------------
	/// Returns the value with the corresponding chain or null if no value is associated with this chain of keys.
	///
	/// @param key - chain of keys and indexes separated by '.' (example: key.key.2.key)
	///
	/// @return Object value
	//------------------------------------------------------------------------------
	public Object GetValueForChain(String keyChain)
	{
		Object result = this;
		String keysLeft = keyChain;
		while (keysLeft.length() > 0)
		{
			String nextKey = nextKey(keysLeft);
			boolean isKeyIndex = IsDigit(nextKey);
			if (isKeyIndex)
			{
				if (result instanceof Object[])
				{
					Object [] array = (Object[])result;
					int index = Integer.parseInt(nextKey);
					if (index < 0 || index >= array.length)
					{
						Log.d(TAG,"JSonObject:GetValue:index is not valid:" + index);
						return null;
					}
					result = array[index];
				}
				else
				{
					isKeyIndex = false;
				}
			}

			if (!isKeyIndex)
			{
				if (!(result instanceof JSonObject))
				{
					Log.d(TAG,"JSonObject:GetValue:json object is not JSonObject:result=" + result);
					return null;
				}
				JSonObject jsonObj = (JSonObject)result;

				int index = jsonObj.GetIndex(nextKey);
				if (index != -1)
				{
					result = jsonObj.m_values[index];
				}
				else
				{
					Log.d(TAG,"JSonObject:GetValue:there is no such key:" + result);
					return null;
				}
			}
			if (keysLeft.length() <= (nextKey.length() + 1))
			{
				break;
			}
			keysLeft = keysLeft.substring(nextKey.length() + 1, keysLeft.length());
		}
		return result;
	}

	//------------------------------------------------------------------------------
	/// Returns the next key in set of keys, what are separated by '.' for example: "key1.key2.4.key3".
	///
	/// @param key - chain of keys and indexes separated by point
	///
	/// @return String next key in the keys hierarchy
	//------------------------------------------------------------------------------
	private String nextKey(String key)
	{
		int dotIndex = key.indexOf('.');
		if (dotIndex < 0)
		{
			return key;
		}
		return key.substring(0, dotIndex);
	}

	//------------------------------------------------------------------------------
	/// Returns the value with the corresponding key
	///
	/// @param key - key to search for
	///
	/// @return int value
	//------------------------------------------------------------------------------
	public int GetInt(String key) throws Exception
	{
		Object o = GetValue(key);
		if (o == null)
		{
			throw new Exception("Key not found: " + key);
		}
		String valueStr = (String)o;

		if (valueStr.indexOf('.') != -1)
		{
			valueStr = valueStr.substring(0, valueStr.indexOf('.'));
		}

		return Integer.parseInt(valueStr);
	}

	//------------------------------------------------------------------------------
	/// Returns the value with the corresponding chain of keys
	///
	/// @param chain - chain of keys to search for
	///
	/// @return int value
	//------------------------------------------------------------------------------
	public int GetIntForChain(String chain) throws Exception
	{
		Object o = GetValueForChain(chain);
		if (o == null)
		{
			throw new Exception("Key not found: " + chain);
		}
		String valueStr = (String)o;

		if (valueStr.indexOf('.') != -1)
		{
			valueStr = valueStr.substring(0, valueStr.indexOf('.'));
		}

		return Integer.parseInt(valueStr);
	}

	//------------------------------------------------------------------------------
	/// Returns the array associated with the key
	///
	/// @param key - key to search for
	///
	/// @return int array
	//------------------------------------------------------------------------------
	public int[] GetIntArray(String key) throws Exception
	{
		Object o = GetValue(key);
		if (o == null)
		{
			throw new Exception("Key not found: " + key);
		}
		Object[] s = (Object[])o;
		int[] arr = new int[s.length];
		for (int i = 0; i < s.length; i++)
		{
			arr[i] = Integer.parseInt((String)s[i]);
		}
		return arr;
	}

	//------------------------------------------------------------------------------
	/// Returns the array associated with the key
	///
	/// @param key - key to search for
	///
	/// @return long array
	//------------------------------------------------------------------------------
	public long[] GetLongArray(String key) throws Exception
	{
		Object o = GetValue(key);
		if (o == null)
		{
			throw new Exception("Key not found: " + key);
		}
		Object[] s = (Object[])o;
		long[] arr = new long[s.length];
		for (int i = 0; i < s.length; i++)
		{
			arr[i] = Long.parseLong((String)s[i]);
		}
		return arr;
	}

	//------------------------------------------------------------------------------
	/// Returns the array associated with the key chain
	///
	/// @param chain - key chain to search for
	///
	/// @return int array
	//------------------------------------------------------------------------------
	public int[] GetIntArrayForChain(String chain) throws Exception
	{
		Object o = GetValueForChain(chain);
		if (o == null)
		{
			throw new Exception("Key not found: " + chain);
		}
		Object[] s = (Object[])o;
		int[] arr = new int[s.length];
		for (int i = 0; i < s.length; i++)
		{
			arr[i] = Integer.parseInt((String)s[i]);
		}
		return arr;
	}

	//------------------------------------------------------------------------------
	/// Returns the string associated with the key
	///
	/// @param key - key to search for
	///
	/// @return String value
	//------------------------------------------------------------------------------
	public String GetString(String key) throws Exception
	{
		Object o = GetValue(key);
		if (o == null)
		{
			throw new Exception("Key not found: " + key);
		}
		return o.toString();
	}

	//------------------------------------------------------------------------------
	/// Returns the string associated with the key chain
	///
	/// @param chain - key chain to search for
	///
	/// @return String value
	//------------------------------------------------------------------------------
	public String GetStringForChain(String chain) throws Exception
	{
		Object o = GetValueForChain(chain);
		if (o == null)
		{
			throw new Exception("Key not found: " + chain);
		}
		return o.toString();
	}

	//------------------------------------------------------------------------------
	/// Returns the array associated with the key
	///
	/// @param key - key to search for
	///
	/// @return String array
	//------------------------------------------------------------------------------
	public String[] GetStringArray(String key) throws Exception
	{
		Object o = GetValue(key);
		if (o == null)
		{
			throw new Exception("Key not found: " + key);
		}
		Object[] s = (Object[])o;
		String[] arr = new String[s.length];
		for (int i = 0; i < s.length; i++)
		{
			arr[i] = (String)s[i];
		}
		return arr;
	}

	//------------------------------------------------------------------------------
	/// Returns the array associated with the key chain
	///
	/// @param chain - key chain to search for
	///
	/// @return String array
	//------------------------------------------------------------------------------
	public String[] GetStringArrayForChain(String chain) throws Exception
	{
		Object o = GetValueForChain(chain);
		if (o == null)
		{
			throw new Exception("Key not found: " + chain);
		}
		Object[] s = (Object[])o;
		String[] arr = new String[s.length];
		for (int i = 0; i < s.length; i++)
		{
			arr[i] = (String)s[i];
		}
		return arr;
	}

	//------------------------------------------------------------------------------
	/// Returns the array associated with the key
	///
	/// @param key - key to search for
	///
	/// @return JSonObject array
	//------------------------------------------------------------------------------
	public JSonObject[] GetJSonArray(String key) throws Exception
	{
		Object o = GetValue(key);
		if (o == null)
		{
			throw new Exception("Key not found: " + key);
		}

		Object[] s = (Object[])o;
		JSonObject[] arr = new JSonObject[s.length];
		for (int i = 0; i < s.length; i++)
		{
			arr[i] = (JSonObject)s[i];
		}
		return arr;
	}

	//------------------------------------------------------------------------------
	/// Returns the array associated with the key chain
	///
	/// @param chain - key chain to search for
	///
	/// @return JSonObject array
	//------------------------------------------------------------------------------
	public JSonObject[] GetJSonArrayForChain(String chain) throws Exception
	{
		Object o = GetValueForChain(chain);
		if (o == null)
		{
			throw new Exception("Key not found: " + chain);
		}

		Object[] s = (Object[])o;
		JSonObject[] arr = new JSonObject[s.length];
		for (int i = 0; i < s.length; i++)
		{
			arr[i] = (JSonObject)s[i];
		}
		return arr;
	}

	//------------------------------------------------------------------------------
	/// Returns the value associated with the key
	///
	/// @param key - key to search for
	///
	/// @return boolean value
	//------------------------------------------------------------------------------
	public boolean GetBoolean(String key) throws Exception
	{
		Object o = GetValue(key);
		if (o == null)
		{
			throw new Exception("Key not found: " + key);
		}
		return ParseBoolean((String)o);
	}

	//------------------------------------------------------------------------------
	/// Returns the value associated with the chain
	///
	/// @param chain - chain of keys to search for
	///
	/// @return boolean value
	//------------------------------------------------------------------------------
	public boolean GetBooleanForChain(String chain) throws Exception
	{
		Object o = GetValueForChain(chain);
		if (o == null)
		{
			throw new Exception("Key not found: " + chain);
		}
		return ParseBoolean((String)o);
	}

	//------------------------------------------------------------------------------
	/// Parses input String as boolean value
	///
	/// @param s - string to parse
	///
	/// @return true if string equals "true". Letter case is ignored.
	//------------------------------------------------------------------------------
	private boolean ParseBoolean(String s)
	{
		return "true".equals(s.toLowerCase());
	}

	//------------------------------------------------------------------------------
	/// Returns the array associated with the key
	///
	/// @param key - key to search for
	///
	/// @return boolean array
	//------------------------------------------------------------------------------
	public boolean[] GetBooleanArray(String key) throws Exception
	{
		Object o = GetValue(key);
		if (o == null)
		{
			throw new Exception("Key not found: " + key);
		}
		String[] s = (String[])o;
		boolean[] arr = new boolean[s.length];
		for (int i = 0; i < s.length; i++)
		{
			arr[i] = ParseBoolean(s[i]);
		}
		return arr;
	}

	//------------------------------------------------------------------------------
	/// Returns the array associated with the chain of keys
	///
	/// @param chain - chain of keys to search for
	///
	/// @return boolean array
	//------------------------------------------------------------------------------
	public boolean[] GetBooleanArrayForChain(String chain) throws Exception
	{
		Object o = GetValueForChain(chain);
		if (o == null)
		{
			throw new Exception("Key not found: " + chain);
		}
		String[] s = (String[])o;
		boolean[] arr = new boolean[s.length];
		for (int i = 0; i < s.length; i++)
		{
			arr[i] = ParseBoolean(s[i]);
		}
		return arr;
	}

	//------------------------------------------------------------------------------
	/// Check if specified Object is digit
	///
	/// @param key - key to search for
	///
	/// @return true if Object.toString() is digit
	//------------------------------------------------------------------------------
	private static boolean IsDigit(Object o)
	{
		boolean res = true;
		String str = o.toString();

		if (str.length() == 0)
		{
			return false;
		}

		for (int i = 0; i < str.length(); i++)
		{
			if (!Character.isDigit(str.charAt(i)))
			{
				res = false;
				break;
			}
		}

		return res;
	}

	//------------------------------------------------------------------------------
	/// Appends specified object to StringBuffer taking into account JSon syntax
	///
	/// @param sb - string buffer to append to
	/// @param o - object buffer to append
	//------------------------------------------------------------------------------
	private void SBAppendObject(StringBuffer sb, Object o)
	{
		boolean appendQuotes = o.toString().equals(o) && !IsDigit(o) && !o.toString().startsWith("{")  && !o.toString().startsWith("[");

		if (appendQuotes)
		{
			sb.append("\"");
		}
		sb.append(o);
		if (appendQuotes)
		{
			sb.append("\"");
		}
	}


	private static final boolean k_isDebug = false;

	//------------------------------------------------------------------------------
	/// Creates JSon string from current JSonObject
	//------------------------------------------------------------------------------
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		if (k_isDebug)
		{
			sb.append("\n");
		}
		for (int i = 0; i < m_maxPairIndex; i++)
		{
			SBAppendObject(sb, m_keys[i]);
			sb.append(": ");
			Object o = m_values[i];
			if (o.getClass().isArray())
			{
				Object arr[] = (Object[])o;
				sb.append("[");
				for (int j = 0; j < arr.length; j++)
				{
					SBAppendObject(sb, arr[j]);
					if (j < arr.length - 1)
					{
						sb.append(", ");
					}
				}
				sb.append("]");
			}
			else
			{
				SBAppendObject(sb, o);
			}
			if (i < m_maxPairIndex - 1)
			{
				sb.append(", ");
			}
			if (k_isDebug)
			{
				sb.append("\n");
			}
		}
		sb.append("}");
		return sb.toString();
	}

	public boolean IsBooleanValue(String key)
	{
		String value = GetValue(key).toString();
		if (value.length() <= 0)
		{
			return false;
		}

		if (value.equals("true") == true || value.equals("false") == true)
		{
			return true;
		}

		return false;
	}

	public boolean IsIntegerValue(String key)
	{
		String value = GetValue(key).toString();
		if (value.length() <= 0)
		{
			return false;
		}

		for (int i = 0; i < value.length(); i++)
		{
			if (value.charAt(i) < '0' || value.charAt(i) > '9')
			{
				return false;
			}
		}

		return true;
	}

	public boolean IsRealValue(String key)
	{
		String value = GetValue(key).toString();
		if (value.length() <= 0)
		{
			return false;
		}

		int countNumber = 0;
		int countPoint = 0;
		for (int i = 0; i < value.length(); i++)
		{
			if (value.charAt(i) >= '0' && value.charAt(i) <= '9')
			{
				countNumber++;
			}
			else if (value.charAt(i) == '.')
			{
				countPoint++;
			}
		}

		if (countPoint <= 1 && countPoint + countNumber == value.length())
		{
			return true;
		}
		return false;
	}

	public boolean IsStringValue(String s)
	{
		return true;
	}

	public boolean IsJsonArrayValue(String key)
	{
		try
		{
			GetJSonArray(key);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public boolean IsIntArrayValue(String key)
	{
		try
		{
			GetIntArray(key);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public boolean IsLongArrayValue(String key)
	{
		try
		{
			GetLongArray(key);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public boolean IsStringArrayValue(String key)
	{
		try
		{
			GetStringArray(key);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public boolean IsBooleanArrayValue(String key)
	{
		try
		{
			GetBooleanArray(key);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}

		return true;
	}

}
